require 'map_editor/map_panel'
require 'map_editor/palet_container'
require 'map_navigator'
module EventEditor
  class EventListPanel < Gtk::VBox
    def initialize
      super
      @model = nil
      @controler = nil
      @list_model = Gtk::ListStore.new(String, String)
      
      sw = Gtk::ScrolledWindow.new
      @list_box = Gtk::TreeView.new(@list_model)
      sw.add(@list_box)
      selection = @list_box.selection
      selection.signal_connect('changed') do
        if selection.selected
          @model.select_event_by_index(selection.selected.path.indices[0])
          @controler.refresh_screen
        end
      end
      
      self.add_columns(@list_box)
      vbox = Gtk::VBox.new
      vbox.set_border_width(5)
      vbox.pack_start(sw, true, 0)
      self.pack_start(vbox, true, true, 0)
    end

    def controler=(controler)
      @controler = controler
    end
    
    def grab_focus
      @list_box.grab_focus
    end
    
    def model=(model)
      @model = model
      @list_model.clear
      self.refresh
    end
    
    def add_columns(treeview)
      # column for fixed toggles
      renderer = Gtk::CellRendererText.new

      column = Gtk::TreeViewColumn.new('ID', renderer, "text" => 0)
      column.sizing = Gtk::TreeViewColumn::FIXED
      column.fixed_width = 50
      
      treeview.append_column(column)

      column = Gtk::TreeViewColumn.new('Name', renderer, "text" => 1)
      treeview.append_column(column)
    end

    def select_row(no)
      if no
        @list_box.selection.select_path(Gtk::TreePath.new(no))
      else
        @list_box.selection.unselect_all
      end
    end
  
    def refresh
      return unless @model
      @list_model.clear
      @model.events.each do |event|
        iter = @list_model.append
        iter[0] = event['id'].to_s
        iter[1] = event['name']
      end
    end
  
  end
end